# !/user/bin/env python3
# -*- encoding: utf-8 -*-
'''
@File    :   convert_img.py
@Time    :   2020/09/17 19:45:22
@Author  :   cecil-y 
@Version :   1.0
@Desc    :   None

登录验证码识别

'''

# here put the import lib
import sys,os
from PIL import Image

class ConvertImg(object):

    # 二值数组
    t2val = {}

    def twoValue(self, image, G):
        for y in range(0, image.size[1]):
            for x in range(0, image.size[0]):
                g = image.getpixel((x, y))
                for z in g:
                    if z > G:
                        self.t2val[(x, y)] = 1
                    else:
                        self.t2val[(x, y)] = 0

    # 根据一个点A的RGB值，与周围的8个点的RBG值比较，设定一个值N（0 <N <8），当A的RGB值与周围8个点的RGB相等数小于N时，此点为噪点
    # G: Integer 图像二值化阀值
    # N: Integer 降噪率 0 <N <8
    # Z: Integer 降噪次数
    # 输出
    #  0：降噪成功
    #  1：降噪失败
    def clearNoise(self, image, N, Z):
        for i in range(0, Z):
            self.t2val[(0, 0)] = 1
            self.t2val[(image.size[0] - 1, image.size[1] - 1)] = 1

            for x in range(1, image.size[0] - 1):
                for y in range(1, image.size[1] - 1):
                    nearDots = 0
                    L = self.t2val[(x, y)]
                    if L == self.t2val[(x - 1, y - 1)]:
                        nearDots += 1
                    if L == self.t2val[(x - 1, y)]:
                        nearDots += 1
                    if L == self.t2val[(x - 1, y + 1)]:
                        nearDots += 1
                    if L == self.t2val[(x, y - 1)]:
                        nearDots += 1
                    if L == self.t2val[(x, y + 1)]:
                        nearDots += 1
                    if L == self.t2val[(x + 1, y - 1)]:
                        nearDots += 1
                    if L == self.t2val[(x + 1, y)]:
                        nearDots += 1
                    if L == self.t2val[(x + 1, y + 1)]:
                        nearDots += 1

                    if nearDots < N:
                        self.t2val[(x, y)] = 1                
    pass

if __name__ == '__main__':
    pass